package cn.com.dragonpass.infra.settle.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestTemplateConfig {

    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
        return new RestTemplate(factory);
    }

    @Bean
    @Primary
    public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        //超时设置
        factory.setReadTimeout(5000);
        factory.setConnectTimeout(15000);
        return factory;
    }
}
package cn.com.dragonpass.infra.settle.util;

import cn.com.dragonpass.common.utils.DateUtils;
import cn.com.dragonpass.infra.accounting.dto.PushWeaverDto;
import cn.com.dragonpass.infra.settle.config.WeaverConfig;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 推送泛微
 */
@Service
@Slf4j
public class WeaverPushUtil {

    @Autowired
    private WeaverConfig weaverConfig;

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 推送泛微
     * @param apiPath 接口地址
     * @param data 参数
     */
    public String push(String apiPath, List<? extends Object> data) {
        Date now = new Date();
        String date = DateUtils.formatDate(now,"yyyy-MM-dd");
        String time = DateUtils.formatDate(now,"HH:mm:ss");
        String url = weaverConfig.getHost() + apiPath;

        // 填充参数
        PushWeaverDto.Operationinfo operationinfo = PushWeaverDto.Operationinfo.builder().operator(weaverConfig.getOperator()).operationDate(date).operationTime(time).build();
        List<PushWeaverDto.HttpParamData> param = new ArrayList<>();
        for(Object o : data){
            PushWeaverDto.HttpParamData p = new PushWeaverDto.HttpParamData();
            p.setOperationinfo(operationinfo);
            p.setMainTable(o);
            param.add(p);
        }
        // 填充token信息
        String yyyyMMddHHmmss = DateUtils.formatDate(now, "yyyyMMddHHmmss");
        PushWeaverDto.Header header = PushWeaverDto.Header.builder()
                .systemid(weaverConfig.getAccount())
                .currentDateTime(yyyyMMddHHmmss)
                .Md5(getMD5(yyyyMMddHHmmss))
                .build();
        PushWeaverDto.HttpParamDto build = PushWeaverDto.HttpParamDto.builder().data(param).header(header).build();
        String jsonParam = JSONObject.toJSONString(build);
        String encodedJsonParam = new String(jsonParam.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8);

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType("application/x-www-form-urlencoded;charset=UTF-8"));
        //headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        log.info("推送泛微接口  param：{}   header{}", encodedJsonParam,headers);
        // 创建 HttpEntity
        HttpEntity<String> entity = new HttpEntity<>("datajson=" + encodedJsonParam, headers);

        // 发送 POST 请求
        ResponseEntity<String> response = restTemplate.postForEntity(
                url,
                entity,
                String.class);
        if(JSONObject.parseObject(response.getBody()).getInteger("status")!=1){
            log.error("推送泛微笑数据返回异常 message:{}", response.getBody());
            return response.getBody();
        }else{
            log.info("推送泛微接口成功");
            return null;
        }
    }

    private String getMD5(String time){
        return DigestUtils.md5DigestAsHex((weaverConfig.getAccount()+weaverConfig.getPwd()+time).getBytes(StandardCharsets.UTF_8));
    }
}
